package com.diydao.dao;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

public class IUserDaosImpl  implements IUserDaos{
    public SqlSessionFactory factory;
    public IUserDaosImpl(SqlSessionFactory factory){
        this.factory=factory;
    }
    public List<User> findall() {
       //1根据sqlfactory获取Sqlsession对象
        SqlSession sqlSession=factory.openSession();
        //2调用Sqlsession的方法
        List<User> userList=sqlSession.selectList("com.diydao.dao.IUserDaos.findall");//能获取的配置信息id
        //3释放资源
        sqlSession.close();
        return  userList;

    }

    public void SaveUser(User user) {
        //1根据sqlsessionfactory获取sqlsession对象
        SqlSession sqlSession=factory.openSession();
        sqlSession.insert("com.diydao.dao.IUserDaos.SaveUser",user);//需要给传参 否则会报错
        sqlSession.commit();
        sqlSession.close();
    }

    public void UpdateUser(User user) {
        //1根据sqlsessionfactory获取sqlsession对象
        SqlSession sqlSession=factory.openSession();
        sqlSession.update("com.diydao.dao.IUserDaos.UpdateUser",user);
        sqlSession.commit();
        sqlSession.close();
    }

    public void delUser(Integer id) {
        //1根据sqlsessionfactory获取sqlsession对象
        SqlSession sqlSession=factory.openSession();
        sqlSession.delete("com.diydao.dao.IUserDaos.delUser",id);
        sqlSession.commit();
        sqlSession.close();
    }

    public User findByid(Integer id) {
        //1根据sqlsessionfactory获取sqlsession对象
        SqlSession sqlSession=factory.openSession();
        User user= sqlSession.selectOne("com.diydao.dao.IUserDaos.findByid",id);
        return user;
    }

    public List<User> linkUser(String username) {
        //1根据sqlsessionfactory获取sqlsession对象
        SqlSession sqlSession=factory.openSession();
        List<User> userList=sqlSession.selectList("com.diydao.dao.IUserDaos.linkUser",username);
        return userList;
    }

    public int findTotal() {
        return 0;
    }

    public int countboy(String sex) {
        return 0;
    }
}
